﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using UtilLib;
using ServerBase.Protocol;
using ServerBase.Config;
using ServerBase.LunarSocket;
using System.Collections.Generic;
using static ServerBase.Service.BaseDispatch;
using static ServerBase.Config.Conf;
using UtilPublic.ServerLoger;
using UtilPublic.Security;
using ServerGame.Service;
using ServerBase.Other;
using System;
using System.Threading;
using ServerBase.Service;
using static ServerGame.Service.Dispatcher;
using ServerBase.Client;
using static UtilPublic.Redis.ServerRedis;
using static ServerBase.DataBaseRedis.RdsTxt;
using static UtilPublic.Serialization.Jsh;
using ServerBase.IniFile;
using System.Text;
using static ServerBase.IniFile.IniConf;
using ServerPublic.Manager;
using ServerGame.GameObject;
using ServerPublic.Data;
using System.Linq;
using UtilPublic.Serialization;
using ServerPublic.DataBaseMysql;
using UtilPublic.ApiDispatcher;
using ServerGame.Data;

namespace ServerGame.Manager
{
    /// <summary>
    /// 游戏服务器GM管理器
    /// </summary>
    public static partial class GmManager
    {
        public static bool 检测账号(B2G_Gt_Action Req, G2B_Gt_Action Rsp)
        {
            ThisSession.Account = Req.Shuttle;
            if (!BaseServerInfo.CfgPlatform.ListGm.Contains(ThisSession.Account))
            {
                Rsp.Result = EProtocolResult.管理员登陆账号错误; Send(Rsp); return false;
            }
            return true;
        }

        public static bool 判断玩家ID(string uid, out Player player)
        {
            player = null;
            if (uid.IsNullOrEmpty())
            {
                return false;
            }
            var puid = uid.ToLong();
            if (puid == 0)
            {
                puid = PubPlayerMgr.GetPuid(uid);
                if (puid == 0)
                {
                    return false;
                }
                else
                {
                    if (DataGame.GetPlayer(puid, out player, EGetPlayerSource.Database))
                    {
                        return true;
                    }
                }
            }
            else
            {
                if (DataGame.GetPlayer(puid, out player, EGetPlayerSource.Database))
                {
                    return true;
                }
            }
            return false;
        }
        
    }
    public partial class GmDispatcherMgr
    {
        [ApiDpGm((int)EGtAction.玩家数据修改)]
        public (EProtocolResult, string) 玩家数据修改(string ReqGtArgs)
        {
            var GtArgs = Jsh.Jobj<CLS_GtPlayerEdit>(ReqGtArgs);

            string puid = GtArgs.Puid;
            if (!GmManager.判断玩家ID(puid, out var player)) { return (EProtocolResult.玩家不存在, ""); }

            var dtNow = DateTime.Now;

            string ArgsStr = GtArgs.EditArg;
            int ArgsInt = GtArgs.EditArg.ToInt();
            long ArgsLong = GtArgs.EditArg.ToLong();
            double ArgsDouble = GtArgs.EditArg.ToDouble();
            List<string> ListArgsString = GtArgs.EditArg.ToListString();
            List<int> ListArgsInt = GtArgs.EditArg.ToListInt();
            List<long> ListArgsLong = GtArgs.EditArg.ToListLong();

            EGtPlayerEdit GtArgsEditMode = EGtPlayerEdit.获取金币;
            if (!GtArgsEditMode.TryParseEnum(GtArgs.EditMode, out GtArgsEditMode))
            {
                return (EProtocolResult.错误的修改类型, "");
            }
            switch (GtArgsEditMode)
            {
                case EGtPlayerEdit.获取金币:
                    player.ValueAdd(EAwardType.金币, ArgsLong, EActionSource.GM);
                    break;
                case EGtPlayerEdit.获取钻石:
                    player.ValueAdd(EAwardType.钻石, ArgsLong, EActionSource.GM);
                    break;
                case EGtPlayerEdit.获取经验:
                    player.ValueAdd(EAwardType.经验, ArgsLong, EActionSource.GM);
                    break;
                case EGtPlayerEdit.获取潜能:
                    player.ValueAdd(EAwardType.潜能, ArgsLong, EActionSource.GM);
                    break;

                case EGtPlayerEdit.满血:
                    player.Full();
                    break;

                case EGtPlayerEdit.增加气血:
                    player[EProp.kee] += ArgsInt;
                    break;
                case EGtPlayerEdit.增加精神:
                    player[EProp.sen] += ArgsInt;
                    break;
                case EGtPlayerEdit.增加内力:
                    player[EProp.force] += ArgsInt;
                    break;
                case EGtPlayerEdit.增加法力:
                    player[EProp.mana] += ArgsInt;
                    break;
                case EGtPlayerEdit.增加食物:
                    player[EProp.food] += ArgsInt;
                    break;
                case EGtPlayerEdit.增加饮水:
                    player[EProp.water] += ArgsInt;
                    break;

                case EGtPlayerEdit.变更门派:
                    if (ConfFamily.ContainsKey(ArgsStr))
                    {
                        player.DaBasic.Family = ArgsStr;
                    }
                    break;

                default:
                    break;
            }

            player.InfoAll();
            player.SaveOffline();

            (ERecordType.管理员修改玩家数据, EActionSource.系统).Rec(0, GtArgs.Puid, GtArgs.EditMode, GtArgs.EditArg);

            return (EProtocolResult.成功, "");
        }
        [ApiDpGm((int)EGtAction.玩家数据查询基本信息)]
        public (EProtocolResult, string) 玩家数据查询基本信息(string ReqGtArgs)
        {
            string puid = ReqGtArgs;
            if (!GmManager.判断玩家ID(puid, out var player)) { return (EProtocolResult.玩家不存在, ""); }
            var RspArgs = InfoMgr.GetInfoBase(player);

            return (EProtocolResult.成功, Jsh.Jstr(RspArgs));
        }
        [ApiDpGm((int)EGtAction.玩家数据查询杂项信息)]
        public (EProtocolResult, string) 玩家数据查询杂项信息(string ReqGtArgs)
        {
            string puid = ReqGtArgs;
            if (!GmManager.判断玩家ID(puid, out var player)) { return (EProtocolResult.玩家不存在, ""); }
            var RspArgs = InfoMgr.GetInfoMisc(player);

            return (EProtocolResult.成功, Jsh.Jstr(RspArgs));
        }
        [ApiDpGm((int)EGtAction.玩家数据查询账号信息)]
        public (EProtocolResult, string) 玩家数据查询账号信息(string ReqGtArgs)
        {
            string puid = ReqGtArgs;
            if (!GmManager.判断玩家ID(puid, out var player)) { return (EProtocolResult.玩家不存在, ""); }
            var RspArgs = new CLS_GtPlayerInfoUser();

            var Account = player.Account;
            if (PubUserMgr.GetUser(Account, out var user))
            {
                RspArgs.Puid = player.Puid;
                RspArgs.Uuid = user.Uuid;
                RspArgs.Platform = (int)user.Platform;
                RspArgs.Account = user.Account;
                RspArgs.Password = user.Password;
                RspArgs.TimeRegister = user.TimeRegister;
                RspArgs.TimeLogin = user.TimeLogin;
            }

            return (EProtocolResult.成功, Jsh.Jstr(RspArgs));
        }
        [ApiDpGm((int)EGtAction.玩家数据查询游戏信息)]
        public (EProtocolResult, string) 玩家数据查询游戏信息(string ReqGtArgs)
        {
            string puid = ReqGtArgs;
            if (!GmManager.判断玩家ID(puid, out var player)) { return (EProtocolResult.玩家不存在, ""); }
            var RspArgs = InfoMgr.GetInfoGame(player);

            return (EProtocolResult.成功, Jsh.Jstr(RspArgs));
        }
        [ApiDpGm((int)EGtAction.玩家数据查询原始数据)]
        public (EProtocolResult, string) 玩家数据查询原始数据(string ReqGtArgs)
        {
            string puid = ReqGtArgs;
            if (!GmManager.判断玩家ID(puid, out var player)) { return (EProtocolResult.玩家不存在, ""); }

            return (EProtocolResult.成功, Jsh.Jstr(player));
        }

    }
}